{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<a href=\"https://www.packtpub.com/big-data-and-business-intelligence/machine-learning-opencv\" target=\"_blank\"><img align=\"left\" src=\"data/cover.jpg\" style=\"width: 76px; height: 100px; background: white; padding: 1px; border: 1px solid black; margin-right:10px;\"></a>\n",
    "*This notebook contains an excerpt from the book [Machine Learning for OpenCV](https://www.packtpub.com/big-data-and-business-intelligence/machine-learning-opencv) by Michael Beyeler.\n",
    "The code is released under the [MIT license](https://opensource.org/licenses/MIT),\n",
    "and is available on [GitHub](https://github.com/mbeyeler/opencv-machine-learning).*\n",
    "\n",
    "*Note that this excerpt contains only the raw code - the book is rich with additional explanations and illustrations.\n",
    "If you find this content useful, please consider supporting the work by\n",
    "[buying the book](https://www.packtpub.com/big-data-and-business-intelligence/machine-learning-opencv)!*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [Representing images](04.05-Representing-Images.ipynb) | [Contents](../README.md) | [Building Our First Decision Tree](05.01-Building-Our-First-Decision-Tree.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using Decision Trees to Make a Medical Diagnosis\n",
    "\n",
    "Now that we know how to handle data in all shapes and forms, be it numerical, categorical,\n",
    "text, or image data, it is time to put our newly gained knowledge to good use.\n",
    "\n",
    "In this chapter, we will learn how to build a machine learning system that can make a\n",
    "medical diagnosis. We aren't all doctors, but we've probably all been to one at some point in\n",
    "our lives. Typically, a doctor would gain as much information as possible about a patient's\n",
    "history and symptoms in order to make an informed diagnosis. We will mimic a doctor's\n",
    "decision-making process with the help of what is known as decision trees.\n",
    "\n",
    "A **decision tree** is a simple yet powerful supervised learning algorithm that resembles a\n",
    "flow chart; we will talk more about this in just a minute. Other than in medicine, decision\n",
    "trees are commonly used in fields such as astronomy (for example, for filtering noise from\n",
    "Hubble Space Telescope images or to classify star-galaxy clusters), manufacturing and\n",
    "production (for example, by Boeing to discover flaws in the manufacturing process), and\n",
    "object recognition (for example, for recognizing 3D objects).\n",
    "\n",
    "Specifically, we want to address the following questions in this chapter:\n",
    "- How do we build simple decision trees from data, and use them for either classification or regression?\n",
    "- How do we decide which decision to make next?\n",
    "- How do we prune a tree, and what is that good for?\n",
    "\n",
    "\n",
    "## Outline\n",
    "\n",
    "- [Building Your First Decision Tree](05.01-Building-Your-First-Decision-Tree.ipynb)\n",
    "- [Using Decision Trees to Diagnose Breast Cancer](05.02-Using-Decision-Trees-to-Diagnose-Breast-Cancer.ipynb)\n",
    "- [Using Decision Trees for Regression](05.03-Using-Decision-Trees-for-Regression.ipynb)\n",
    "\n",
    "\n",
    "\n",
    "But first, let's talk about what decision trees actually are."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Understanding Decision Trees\n",
    "\n",
    "Decision trees are simple yet powerful model for supervised learning problems. Like the name suggests, you can think of them as a tree in which information flows along different branches in the tree - starting at the trunk, and going all the way to the individual leaves.\n",
    "\n",
    "> Here the book offers a detailed treatment of the inner workings of decision trees, along with illustrations and simple examples. For more information on decision trees, please refer to the book.\n",
    "\n",
    "Let's say we have a dataset consisting of a single e-mail:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = [\n",
    "    'I am Mohammed Abacha, the son of the late Nigerian Head of '\n",
    "    'State who died on the 8th of June 1998. Since i have been '\n",
    "    'unsuccessful in locating the relatives for over 2 years now '\n",
    "    'I seek your consent to present you as the next of kin so '\n",
    "    'that the proceeds of this account valued at US$15.5 Million '\n",
    "    'Dollars can be paid to you. If you are capable and willing '\n",
    "    'to assist, contact me at once via email with following '\n",
    "    'details: 1. Your full name, address, and telephone number. '\n",
    "    '2. Your Bank Name, Address. 3.Your Bank Account Number and '\n",
    "    'Beneficiary Name - You must be the signatory.'\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This data can be vectorized using Scikit-Learn's `CountVectorizer`, which turns the e-mail into its individual words and corresponding word counts:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "vec = CountVectorizer()\n",
    "X = vec.fit_transform(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's look at the first five words in the dictionaries, and their word counts:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['15', '1998', '8th', 'abacha', 'account']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vec.get_feature_names()[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 1, 1, 2], dtype=int64)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.toarray()[0, :5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So, how would you have checked if the e-mail is from a Nigerian prince?\n",
    "\n",
    "One way to do this is to look if the e-mail contained both the words 'nigerian' and 'prince':"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'nigerian' in vec.get_feature_names()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'prince' in vec.get_feature_names()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "And what do we find to our surprise? The word 'prince' does not occur in the e-mail.\n",
    "\n",
    "Does this mean the message is legit?\n",
    "\n",
    "No, of course not. Instead of 'prince', the e-mail went with the words 'head of state' instead - effectively circumventing our all-too-simple spam detector.\n",
    "\n",
    "Luckily, the theoretical framework behind decision trees provides us with help with both finding the right decision rules as well as which decisions to tackle next."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [Representing images](04.05-Representing-Images.ipynb) | [Contents](../README.md) | [Building Our First Decision Tree](05.01-Building-Our-First-Decision-Tree.ipynb) >"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
